<template>
  <a-card title="会员活动中心" :bordered="false" class="br-10">
    <template #extra>
      <a-button type="primary" size="default" @click="handleAdd">新增</a-button>
    </template>
    <div class="table-page-search">
      <a-form>
        <a-form-item label="关键字">
          <a-input placeholder="请输入" v-model="queryParam.keyWord" />
        </a-form-item>
        <span class="search-submitButtons">
          <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
          <a-button @click="() => (queryParam = { keyWord: '' })">重置</a-button>
        </span>
      </a-form>
    </div>
    <s-table size="default" ref="table" rowKey="userId" :rowSelection="null" :columns="columns" :data="loadData">
      <span slot="serial" slot-scope="text, record, index">
        {{ (pageNum - 1) * pageSize + index + 1 }}
      </span>
      <span slot="icon" slot-scope="text">
        <ImgOrVideo :item="{url: text}" preview :width="80" :height="80" />
      </span>
      <span slot="addressAll" slot-scope="text, record">
        {{ record.provinceName + record.cityName + record.areaName }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a-button type="primary" ghost size="small" class="_mr-5" @click="handleEdit(record)">
          编辑
        </a-button>
        <a-button type="primary" ghost size="small" class="_mr-5" @click="delByIds([record.id])">
          删除
        </a-button>
      </span>
    </s-table>
    <memberActivitiesForm ref="memberActivitiesForm" @add="$refs.table.refresh(true)" @upd="$refs.table.refresh()" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { centerGetList, centerDel } from '@/api/web/biz'
import memberActivitiesForm from './memberActivitiesForm'
import ImgOrVideo from '@/components/ImgOrVideo';
export default {
  components: {
    STable,
    ImgOrVideo,
    memberActivitiesForm
  },
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          width: '80px'
        },
        {
          title: '图标',
          dataIndex: 'icon',
          scopedSlots: { customRender: 'icon' }
        },
        {
          title: '名称',
          dataIndex: 'name',
          ellipsis: true
        },
        {
          title: '地区',
          dataIndex: 'addressAll',
          ellipsis: true,
          scopedSlots: { customRender: 'addressAll' }
        },
        {
          title: '地址',
          dataIndex: 'address',
          ellipsis: true
        },
        {
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      pageNum: 1,
      pageSize: 10,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        var params = {
          current: parameter.pageNum,
          size: parameter.pageSize,
          branchId: this.$route.query.id,
          type: 1
        }
        return centerGetList(Object.assign(params, this.queryParam)).then(res => {
          this.pageNum = parameter.pageNum
          this.pageSize = parameter.pageSize
          this.selectedRowKeys = []
          this.selectedRows = []
          this.ids = []
          return res
        })
      }
    }
  },
  created() {},
  methods: {
    handleAdd() {
      var params = {
        title: '新增活动中心',
        type: 'add'
      }
      this.$refs.memberActivitiesForm.add(params)
    },

    handleEdit(record) {
      var obj = JSON.parse(JSON.stringify(record))
      obj.areaIds = obj.provinceId + obj.cityId + obj.areaId
      var params = {
        title: '编辑活动中心',
        type: 'edit',
        data: obj
      }
      this.$refs.memberActivitiesForm.edit(params)
    },

    handleOk() {
      this.$refs.table.refresh(true)
    },
    handleReload() {
      this.$refs.table.refresh()
    },
    delByIds(ids) {
      var _this = this
      this.$confirm({
        title: '删除?',
        content: '是否确定删除该活动?',
        onOk() {
          centerDel({ ids: ids.join(',') }).then(res => {
            _this.$message.success(`删除成功`)
            _this.handleReload()
          })
        },
        onCancel() {}
      })
    }
  }
}
</script>
<style scoped lang="less"></style>
